# -*- coding:utf-8 -*-

# 函数嵌套
def func1():
    print('func1')
    def func2() :
        print('func2')

func1() # func2 不会运行

# 作用域
x = 0
def func2():
    x = 1
    def func3():
        x = 2
        print(x) # 2
    func3()

func2()

# 高阶函数
def func3(func) :
    print(func())
    return  func;

def func4():
    return 'abc'
func3 = func3(func4)
func3()

# 装饰器
import time
def timer(func):
    def decorator(*args, **kwargs):
        start_time = time.time()
        func(*args,**kwargs)
        end_time = time.time()
        print('this func run time is %s'%(end_time - start_time))
    return  decorator

@timer # func5 = timer(func5)
def func5(x):
    return x**2
func5(3)

# 装饰器2
uname, passwd = 'guochen','a'
def auth(func):
    def wrapper(*args, **kwargs):
        input_name = input('please input your name:')
        input_pwd = input('please intpu you password:')
        if (input_name == uname and input_pwd == passwd):
            res = func(*args, **kwargs)
        else:
            exit('sorry!')
        return  res
    return wrapper

@auth
def index(num):
    print('is index')
    return num

def home():
    print('home')
    return 'home'

print(index(123))

# 装饰器3

def auth2(auth_type):
    def out_wrapper(func) :
        def wrapper(*args, **kwargs):
            res = func(*args, **kwargs)
            return  res
        return wrapper
    return out_wrapper

@auth2(auth_type='local') #  index2 = auth2(auth_type)(index2)
def index2(num):
    print('index')
    return  num

@auth2(auth_type='remote')
def home2():
    print('home')
    return 'home'

print('装饰器3 ',index2(1), home2())
